package com.kungfuhacking.wristbandpro.location.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.location.bean.WhiteListEntity;
import com.kungfuhacking.wristbandpro.location.presenter.AffectionAdapter;
import com.kungfuhacking.wristbandpro.location.presenter.IAffectionPresenter;
import com.kungfuhacking.wristbandpro.location.view.IAffectionView;
import com.kungfuhacking.wristbandpro.widget.swipe.DividerItemDecoration;
import com.kungfuhacking.wristbandpro.widget.swipe.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 亲情号码页面
 * Created by Aaron on 17/4/26.
 */
public class AffectionActivity extends BaseActivity implements IAffectionView, View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    private TitleBarView tbv;
    private android.support.v7.widget.RecyclerView recycleView;
    private android.widget.ImageView iv_add;
//    private android.widget.TextView tvadd;
    private List<WhiteListEntity> affectionBeanList = new ArrayList<>();
    private IAffectionPresenter iAffectionPresenter = new IAffectionPresenter(this);
    private AffectionAdapter affectionAdapter;
    private String TAG = getClass().getSimpleName();
    private EditText et_tel;
    private EditText et_nickname;
    private Dialog whiteListDialog;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_white_tel);
        initWidget();
        iAffectionPresenter.getAffectionList();
    }

    private void initWidget() {
//        this.tvadd = (TextView) findViewById(R.id.tv_add);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        //为SwipeRefreshLayout设置监听事件
        swipeRefreshLayout.setOnRefreshListener(this);
//        //为SwipeRefreshLayout设置刷新时的颜色变化，最多可以设置4种
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        this.iv_add = (ImageView) findViewById(R.id.iv_add);
        this.recycleView = (RecyclerView) findViewById(R.id.rv_list);
        this.tbv = (TitleBarView) findViewById(R.id.tbv);
        initRecycleView();
        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        iv_add.setOnClickListener(this);
        tbv.setTitle(getResources().getString(R.string.affection_num));
    }

    private void initRecycleView() {

        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setItemAnimator(new DefaultItemAnimator());
        affectionAdapter = new AffectionAdapter(this, affectionBeanList);
        recycleView.setAdapter(affectionAdapter);
        recycleView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST,
                getResources().getDrawable(R.color.bg_gray)));
        recycleView.addOnItemTouchListener(new OnItemClickListener(recycleView) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, int position) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//dragFlags的时候，就会在长按item的时候进入拖动模式，然后就会一直回调onMove函数。我们可以在onMove中进行数据集的更新
                final int dragFlags;//用于拖拽的标志 如果是网格布局，拖动方向为上下左右，否则只有上下方向
                final int swipeFlags;//滑动标记

                if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                    swipeFlags = 0;
                } else {
//                    dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                    dragFlags = 0;
                    swipeFlags = ItemTouchHelper.START;
//                    ItemTouchHelper.START | ItemTouchHelper.END;
                }
                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {

//                LogUtil.d(TAG, "=== onMove ====== ");
//                int fromPosition = viewHolder.getAdapterPosition();
//                int toPosition = target.getAdapterPosition();
//
//                LogUtil.e(TAG, "拖动listview " + fromPosition + ", to : " + toPosition);
//                data.add(toPosition, data.remove(fromPosition));
//                adapter.notifyItemMoved(fromPosition, toPosition);
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                int position = viewHolder.getAdapterPosition();

//                LogUtil.d(TAG, "=== onSwiped ====== " + position);
                affectionBeanList.remove(position);
                affectionAdapter.notifyItemRemoved(position);
                //更新白名单
                iAffectionPresenter.updateAffectionList(affectionBeanList);

            }

        });
        itemTouchHelper.attachToRecyclerView(recycleView);
    }

    /**
     * 更新亲情号码时, 重新更新下数据
     */
    @Override
    public void notifyInfo(List<WhiteListEntity> whiteList) {
        this.affectionBeanList = whiteList;
        affectionAdapter.setAffectionBeanList(affectionBeanList);
        affectionAdapter.notifyDataSetChanged();
    }

    @Override
    public String getAffectionNum() {
        return et_tel.getText().toString();
    }

    @Override
    public String getAffectionName() {
        return et_nickname.getText().toString();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.iv_add://添加亲情号码
                addAffenctionNum();
                break;

            case R.id.tv_create://创建白名单
                if (iAffectionPresenter.checkAffectionInput(affectionBeanList))
                    dismissDialog();
                break;

            case R.id.tv_cancle://取消
                dismissDialog();
                break;

        }
    }

    private void dismissDialog() {
        if (whiteListDialog != null && whiteListDialog.isShowing())
            whiteListDialog.dismiss();
    }

    /**
     * 添加亲情号码的对话框
     */
    private void addAffenctionNum() {

        whiteListDialog = new Dialog(this, R.style.no_title);
        whiteListDialog.setCancelable(true);
        whiteListDialog.setCanceledOnTouchOutside(true);
        whiteListDialog.show();
        whiteListDialog.setContentView(R.layout.dialog_add_white_list);
        et_tel = (EditText) whiteListDialog.findViewById(R.id.et_tel);
        et_nickname = (EditText) whiteListDialog.findViewById(R.id.et_nickname);
        TextView tv_create = (TextView) whiteListDialog.findViewById(R.id.tv_create);
        TextView tv_cancle = (TextView) whiteListDialog.findViewById(R.id.tv_cancle);

        tv_create.setOnClickListener(this);
        tv_cancle.setOnClickListener(this);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        iAffectionPresenter.clearRequest();
    }

    @Override
    public void onRefresh() {
        affectionBeanList.clear();
        affectionAdapter.setAffectionBeanList(affectionBeanList);
        affectionAdapter.notifyDataSetChanged();
        iAffectionPresenter.getAffectionList();
        //结束后停止刷新
        swipeRefreshLayout.setRefreshing(false);
    }
}
